package com.tgu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgu.entity.model.ApproveEntity;
import com.tgu.entity.model.UsersEntity;
import com.tgu.entity.request.ApproveReq;
import com.tgu.mapper.ApproveDao;
import com.tgu.mapper.UsersDao;
import com.tgu.service.IApproveService;
import com.tgu.service.IUsersService;
import com.tgu.utils.result.Result;
import com.tgu.utils.result.SystemCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApproveServiceImpl implements IApproveService {

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private ApproveDao approveDao;

    @Override
    public Result accept(ApproveReq approveReq) {
        UsersEntity user = usersDao.selectById(approveReq.getUserId());
        if (user == null) {
            return Result.error(SystemCodeEnum.USER_NOT_EXIST);
        }

        UsersEntity manager = usersDao.selectById(approveReq.getManagerId());
        if (manager == null) {
            return Result.error(SystemCodeEnum.USER_NOT_EXIST);
        }

        LambdaQueryWrapper<ApproveEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(true, ApproveEntity::getUserId, approveReq.getUserId());
        ApproveEntity approveEntity = approveDao.selectOne(lqw);
        if (approveEntity == null) {
            return Result.error(SystemCodeEnum.APPROVE_NOT_EXIST);
        }

        approveEntity.setManagerId(approveReq.getManagerId());
        approveEntity.setState(1);
        int i = approveDao.updateById(approveEntity);
        if (i == 0) {
            return Result.error(SystemCodeEnum.APPROVE_ACCEPT_ERROR);
        }

        user.setUserType(1);
        i = usersDao.updateById(user);
        if (i == 0) {
            return Result.error(SystemCodeEnum.APPROVE_ACCEPT_ERROR);
        }

        return Result.ok(SystemCodeEnum.APPROVE_ACCEPT_SUCCESS);
    }

    @Override
    public Result refuse(ApproveReq approveReq) {
        LambdaQueryWrapper<ApproveEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(approveReq.getUserId() != null, ApproveEntity::getUserId, approveReq.getUserId());
        int i = approveDao.delete(lqw);
        if (i == 0) {
            return Result.error(SystemCodeEnum.APPROVE_REFUSE_ERROR);
        }

        return Result.ok(SystemCodeEnum.APPROVE_REFUSE_SUCCESS);
    }

    @Override
    public Result callBack(ApproveReq approveReq) {
        UsersEntity user = usersDao.selectById(approveReq.getUserId());
        if (user == null) {
            return Result.error(SystemCodeEnum.USER_NOT_EXIST);
        }

        UsersEntity manager = usersDao.selectById(approveReq.getManagerId());
        if (manager == null) {
            return Result.error(SystemCodeEnum.USER_NOT_EXIST);
        }

        LambdaQueryWrapper<ApproveEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(true, ApproveEntity::getUserId, approveReq.getUserId());
        ApproveEntity approveEntity = approveDao.selectOne(lqw);
        if (approveEntity == null) {
            return Result.error(SystemCodeEnum.APPROVE_NOT_EXIST);
        }

        approveEntity.setState(0);
        int i = approveDao.updateById(approveEntity);
        if (i == 0) {
            return Result.error(SystemCodeEnum.APPROVE_CALLBACK_ERROR);
        }

        user.setUserType(0);
        i = usersDao.updateById(user);
        if (i == 0) {
            return Result.error(SystemCodeEnum.APPROVE_ACCEPT_ERROR);
        }

        return Result.ok(SystemCodeEnum.APPROVE_CALLBACK_SUCCESS);
    }

    @Override
    public Result getPage(Integer pageNumb, Integer option1, String option2) {
        Page<ApproveEntity> page = new Page<>(pageNumb, 8);

        LambdaQueryWrapper<ApproveEntity> lqw = new LambdaQueryWrapper<>();

        String option = option1.toString() + option2;
        switch (option) {
            case "0a":
                break;
            case "0b":
                lqw.orderByAsc(ApproveEntity::getInfoDate);
                break;
            case "0c":
                lqw.orderByDesc(ApproveEntity::getInfoDate);
                break;
            case "0d":
                lqw.orderByAsc(ApproveEntity::getUserId);
                break;
            case "0e":
                lqw.orderByDesc(ApproveEntity::getUserId);
                break;
            case "1a":
                lqw.eq(true, ApproveEntity::getState, 0);
                break;
            case "1b":
                lqw.eq(true, ApproveEntity::getState, 0)
                        .orderByAsc(ApproveEntity::getInfoDate);
                break;
            case "1c":
                lqw.eq(true, ApproveEntity::getState, 0)
                        .orderByDesc(ApproveEntity::getInfoDate);
                break;
            case "1d":
                lqw.eq(true, ApproveEntity::getState, 0)
                        .orderByAsc(ApproveEntity::getUserId);
                break;
            case "1e":
                lqw.eq(true, ApproveEntity::getState, 0)
                        .orderByDesc(ApproveEntity::getUserId);
                break;
            case "2a":
                lqw.eq(true, ApproveEntity::getState, 1);
                break;
            case "2b":
                lqw.eq(true, ApproveEntity::getState, 1)
                        .orderByAsc(ApproveEntity::getInfoDate);
                break;
            case "2c":
                lqw.eq(true, ApproveEntity::getState, 1)
                        .orderByDesc(ApproveEntity::getInfoDate);
                break;
            case "2d":
                lqw.eq(true, ApproveEntity::getState, 1)
                        .orderByAsc(ApproveEntity::getUserId);
                break;
            case "2e":
                lqw.eq(true, ApproveEntity::getState, 1)
                        .orderByDesc(ApproveEntity::getUserId);
                break;
        }
        approveDao.selectPage(page, lqw);
        List<ApproveEntity> records = page.getRecords();
//        // 先筛选数据
//        switch (option1) {
//            case 0:
//                // 选出全部
//                approveDao.selectPage(page, lqw);
//                records = page.getRecords();
//                break;
//            case 1:
//                // 选出未通过
//                lqw.eq(true, ApproveEntity::getState, 0);
//                approveDao.selectPage(page, lqw);
//                records = page.getRecords();
//                break;
//            case 2:
//                // 选出已通过
//                lqw.eq(true, ApproveEntity::getState, 1);
//                approveDao.selectPage(page, lqw);
//                records = page.getRecords();
//                break;
//        }
//        // 然后做排序
//        switch (option2) {
//            case "a":
//                break;
//            case "b":
//                records.sort((r1, r2) -> (int)(r1.getInfoDate().getTime() - r2.getInfoDate().getTime()));
//                break;
//            case "c":
//                records.sort((r1, r2) -> (int)(r2.getInfoDate().getTime() - r1.getInfoDate().getTime()));
//                break;
//            case "d":
//                records.sort((r1, r2) -> (int)(Long.parseLong(r1.getUserId()) - Long.parseLong(r2.getUserId())));
//                break;
//            case "e":
//                records.sort((r1, r2) -> (int)(Long.parseLong(r2.getUserId()) - Long.parseLong(r1.getUserId())));
//                break;
//        }

        return Result.ok(SystemCodeEnum.SELECT_SUCCESS).setData(records);
    }
}
